Note: This tutorial assumes that you have completed the previous tutorials: Creating Packages. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Using the Buttons
Description: This tutorial shows how to access the state of the buttons on Mini MaxwellTutorial Level: BEGINNER
Next Tutorial: Moving the Base
Overview
There are two buttons on Mini Maxwell, which are easily accessible through ROS.
Under the Hood
This section describes what is happening at a low-level, feel free to skip past it.
Both buttons are connected through the ArbotiX2, as digital inputs. Due to the way they are wired, the buttons are at 5V when unpressed, and pressing the button changes the level to 0V. The ArbotiX uses 8-bit values to encode the voltages, which means that 0V = 0, and 5V = 255 (the largest value which can be stored in an unsigned 8-bit data type).
The Topics
The buttons broadcast on two topics:
/arbotix/green_button /arbotix/yellow_button
You can see the output using the rostopic tool:
$ rostopic echo /arbotix/green_button header: seq: 59 stamp: secs: 1307850081 nsecs: 213567018 frame_id: '' value: 255 direction: 0 --- header: seq: 60 stamp: secs: 1307850081 nsecs: 296992063 frame_id: '' value: 0 direction: 0
Each topic is output at approximately 5 hz. A value of 0 means that the button is pressed, while 255 means it is not. The direction will always be 0 (an input).
Reading the Button State in Python
We will now create a simple program to read the state of the buttons. You should create a package called 'button_test' using roscreate-pkg, be sure to add a dependency on the 'arbotix_msgs' which contains the message definition we will need for reading the buttons. Alternatively, you can find this code in the mini_max_tutorials package.
Then open the file 'button_test.py' in the editor of your choice, and enter the following:
Could not fetch external code from 'https://vanadium-ros-pkg.googlecode.com/svn/trunk/mini_max/mini_max_tutorials/nodes/button_test.py': HTTP Error 404: Not Found
Mark the file executable:
chmod +x button_test.py
And then run it:
rosrun button_test button_test.py
You should then see a message posted that the button is pressed whenever you press it!